package xxy.com.yangproject.ui.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.LogUtils;

import xxy.com.yangproject.R;
import xxy.com.yangproject.adapter.NavigationChildAdapter;
import xxy.com.yangproject.global.Constant;

public class NavigationChildFragment extends Fragment {

    private static String MESSAGE = "show result";
    private FragmentActivity mContext;
    private static int mPosition;

    public static NavigationChildFragment buildFragment(int posoition){
        NavigationChildFragment navigationChildFragment = new NavigationChildFragment();
        Bundle bundle = new Bundle();
        bundle.putString(Constant.MESSAGE, String.valueOf(posoition + 1));
        navigationChildFragment.setArguments(bundle);
        mPosition = posoition;
        return navigationChildFragment;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_navigation_child, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        if (getArguments() != null) {
            ((TextView)view.findViewById(R.id.textView)).setText("第" + getArguments().getString(Constant.MESSAGE) + "个Fragment");
        }
        if (getParentFragment() != null) {
            LogUtils.eTag(Constant.TAG, "父类Fragment的名字： "+getParentFragment().getClass().getName());
        }
        mContext = getActivity();
        RecyclerView recyclerView = view.findViewById(R.id.RecyclerView);
        GridLayoutManager gridLayoutManager = new GridLayoutManager(mContext, 4);
        gridLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
            @Override
            public int getSpanSize(int position) {
                return /*(0 == (recyclerView.getAdapter().getItemViewType(position)) ?
                        gridLayoutManager.getSpanCount() : 1)*/2;
            }
        });
        gridLayoutManager.setSpanCount(5);
        recyclerView.setLayoutManager(gridLayoutManager);
        NavigationChildAdapter navigationChildAdapter;
        recyclerView.setAdapter(navigationChildAdapter = new NavigationChildAdapter(mContext, mPosition));
        //pool ?
        LogUtils.eTag(Constant.TAG, "recyclerview的名称 ： " + recyclerView.getClass().getName());
        recyclerView.setRecycledViewPool(getFragmentPool());
        recyclerView.isLayoutRequested();
//        RecyclerView.ViewHolder recycledView = recyclerView.getRecycledViewPool().getRecycledView(1);
//        if (recycledView != null) {
////            LogUtils.eTag(Constant.TAG, "设置完recycled view pool 后recyclerview的名称： " + recyclerView.getClass().getName());
////            LogUtils.eTag(Constant.TAG, "设置完recycled view pool 后Adapter的Position： " + recycledView.getAdapterPosition());
//            RecyclerView.RecycledViewPool recycledViewPool = recyclerView.getRecycledViewPool();
//
//        }
    }

    private RecyclerView.RecycledViewPool getFragmentPool() {
        RecyclerView.RecycledViewPool recycledViewPool = new RecyclerView.RecycledViewPool();
        recycledViewPool.setMaxRecycledViews(1, 3);
        return recycledViewPool;
    }

}
